<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { Dialog, showDialog } from 'vant'

const router = useRouter()
const user = ref({
  avatar: '',
  username: '',
  phone: ''
})
const active = ref(1) // 默认个人中心

// 设置弹窗
function onSettingClick() {
  showDialog({
    title: '设置',
    message: '确定要退出登录吗？',
    confirmButtonText: '退出登录',
    cancelButtonText: '取消',
    showCancelButton: true
  }).then(() => {
    // 退出登录
    localStorage.removeItem('token')
    localStorage.removeItem('userid')
    user.value.avatar = ''
    user.value.username = '请登录'
    user.value.phone = ''
    router.replace('/login')
  }).catch(() => {
    // 取消
  })
}

onMounted(async () => {
  const token = localStorage.getItem('token')
  const userid = localStorage.getItem('userid')
  if (!token || !userid) {
    user.value.avatar = ''
    user.value.username = '请登录'
    user.value.phone = ''
    return
  }
  try {
    const res = await axios.get(`http://localhost:8000/user/info/?userid=${userid}`, {
      headers: { Authorization: `${token}` }
    })
    const info = res.data.data
    user.value.avatar = info.avatar ? `http://localhost:8000/${info.avatar}` : 'https://img.yzcdn.cn/vant/cat.jpeg'
    user.value.username = info.username || '未设置昵称'
    user.value.phone = info.phone ? info.phone.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2') : ''
  } catch (e) {
    user.value.avatar = ''
    user.value.username = '请登录'
    user.value.phone = ''
  }
})

function handleCellClick(type) {
  if (type === 'patient') {
    router.push('/about')
  } else {
    alert('点击了：' + type)
  }
}

function handleLoginClick() {
  if (user.value.username === '请登录') {
    router.replace('/login')
  }
}
function handleTabChange(index) {
  if (index === 0) {
    router.push('/index')
  }
}
</script>

<template>
  <div class="home-bg">
    <!-- 顶部蓝色背景 -->
    <div class="profile-header">
      <!-- 设置按钮 -->
      <van-icon name="setting-o" class="setting-icon" @click="onSettingClick" />
      <div class="profile-title">个人中心</div>
      <div class="profile-info">
        <img :src="user.avatar" class="avatar" />
        <div class="profile-user">
          <div
            class="username"
            :class="{ clickable: user.username === '请登录' }"
            @click="handleLoginClick"
          >
            {{ user.username }}
          </div>
          <div class="phone">{{ user.phone }}</div>
        </div>
      </div>
    </div>

    <!-- 快捷入口 -->
    <div class="quick-actions">
      <div class="quick-action" @click="handleCellClick('patient')">
        <div class="quick-icon-bg">
          <van-icon name="friends-o" size="28" color="#338bff" />
        </div>
        <div class="quick-label">就诊人管理</div>
      </div>
      <div class="quick-action" @click="handleCellClick('inpatient')">
        <div class="quick-icon-bg">
          <van-icon name="manager-o" size="28" color="#338bff" />
        </div>
        <div class="quick-label">住院人管理</div>
      </div>
    </div>

    <!-- 列表 -->
    <div class="main-content">
      <div class="card-list">
        <div class="card-item" @click="handleCellClick('register')">
          <div class="card-icon-bg"><van-icon name="records" size="22" color="#338bff" /></div>
          <span>预约挂号记录</span>
          <van-icon name="arrow" size="18" color="#ccc" class="arrow" />
        </div>
        <div class="card-item" @click="handleCellClick('pay')">
          <div class="card-icon-bg"><van-icon name="bill-o" size="22" color="#338bff" /></div>
          <span>门诊缴费记录</span>
          <van-icon name="arrow" size="18" color="#ccc" class="arrow" />
        </div>
        <div class="card-item" @click="handleCellClick('outpatient-recharge')">
          <div class="card-icon-bg"><van-icon name="balance-o" size="22" color="#338bff" /></div>
          <span>门诊充值记录</span>
          <van-icon name="arrow" size="18" color="#ccc" class="arrow" />
        </div>
        <div class="card-item" @click="handleCellClick('inpatient-recharge')">
          <div class="card-icon-bg"><van-icon name="balance-list-o" size="22" color="#338bff" /></div>
          <span>住院充值记录</span>
          <van-icon name="arrow" size="18" color="#ccc" class="arrow" />
        </div>
        <div class="card-item" @click="handleCellClick('nucleic')">
          <div class="card-icon-bg"><van-icon name="notes-o" size="22" color="#338bff" /></div>
          <span>核酸预约记录</span>
          <van-icon name="arrow" size="18" color="#ccc" class="arrow" />
        </div>
        <div class="card-item" @click="handleCellClick('checkup')">
          <div class="card-icon-bg"><van-icon name="notes-o" size="22" color="#338bff" /></div>
          <span>体检预约记录</span>
          <van-icon name="arrow" size="18" color="#ccc" class="arrow" />
        </div>
        <div class="card-item" @click="handleCellClick('case')">
          <div class="card-icon-bg"><van-icon name="description" size="22" color="#338bff" /></div>
          <span>病案申请记录</span>
          <van-icon name="arrow" size="18" color="#ccc" class="arrow" />
        </div>
      </div>
    </div>

    <!-- 底部导航栏 -->
    <van-tabbar v-model="active" active-color="#338bff" @change="handleTabChange">
      <van-tabbar-item icon="home-o">首页</van-tabbar-item>
      <van-tabbar-item icon="user-o">个人中心</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<style scoped>
.setting-icon {
  position: absolute;
  left: 18px;
  top: 18px;
  font-size: 26px;
  color: #fff;
  z-index: 10;
  cursor: pointer;
}
.home-bg {
  min-height: 100vh;
  background: #f6f7fb;
  display: flex;
  flex-direction: column;
}
.profile-header {
  background: #338bff;
  border-radius: 0 0 24px 24px;
  padding-bottom: 32px;
  position: relative;
  color: #fff;
  text-align: left;
}
.profile-title {
  font-size: 18px;
  font-weight: 500;
  text-align: center;
  padding-top: 24px;
  letter-spacing: 2px;
}
.profile-info {
  display: flex;
  align-items: center;
  margin-top: 18px;
  padding: 0 24px;
}
.avatar {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: 2px solid #fff;
  background: #fff;
  object-fit: cover;
  margin-right: 18px;
}
.profile-user {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
}
.username {
  font-size: 22px;
  font-weight: bold;
  color: #fff;
  margin-bottom: 6px;
}
.phone {
  font-size: 16px;
  color: #e0e0e0;
}
.quick-actions {
  display: flex;
  justify-content: space-between;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  margin: -32px 18px 18px 18px;
  padding: 18px 0;
  position: relative;
  z-index: 2;
}
.quick-action {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  cursor: pointer;
  color: #338bff;
  font-size: 15px;
}
.quick-action:not(:last-child) {
  margin-right: 0;
}
.quick-icon-bg {
  width: 48px;
  height: 48px;
  background: #f2f6ff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 6px;
}
.quick-label {
  font-size: 15px;
  color: #333;
}
.main-content {
  flex: 1;
  padding: 0 12px 60px 12px;
}
.card-list {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  padding: 0 0 0 0;
}
.card-item {
  display: flex;
  align-items: center;
  padding: 16px 18px;
  border-bottom: 1px solid #f2f2f2;
  font-size: 16px;
  cursor: pointer;
  position: relative;
}
.card-item:last-child {
  border-bottom: none;
}
.card-icon-bg {
  width: 38px;
  height: 38px;
  background: #f2f6ff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 14px;
}
.arrow {
  position: absolute;
  right: 18px;
}
.username.clickable {
  cursor: pointer;
  text-decoration: underline;
}
</style>